home *** CD-ROM | disk | FTP | other *** search
/ CD Ware Multimedia 1995 May / cd Ware (Juegos) Epimundo.iso / DOS / C / WLIB.ZIP / WLINK.DOC < prev    next >
Encoding:
Text File  |  1993-02-12  |  10.2 KB  |  317 lines

  1. Wheaton Library Linked List Documentation
  2.  
  3. February 22, 1991
  4. May 15, 1992 (minor tweaks)
  5. October 21, 1992 (minor update)
  6.  
  7. Copyright (c) 1992, 1993 by Paul Wheaton
  8.  
  9. Note that this documentation does not cover all of the features of the
  10. provided in this library.  For that, you will have to look at the header files.
  11.  
  12. The Wheaton Libraries are shareware. Anyone may use the libraries provided
  13. that they do not change them.  If you want to change them or if you want
  14. phone support from me, you must pay $50. This in the hope that the hundreds
  15. of vendors that are creating their own string libraries can go with just
  16. one - I'm pretty tired of getting a library in that seems like it will be
  17. pretty cool, just to find out that it uses the same identifiers as
  18. something else and there are then link conflicts.
  19.  
  20. To make these libraries available, you need to include <LinkList.h>
  21. and link with WLIB.LIB.
  22.  
  23. 1    what is a linked list:  help for the heapless
  24. 1.1    some references
  25. 1.2    my version
  26. 2    LinkedList class
  27. 2.01    Root()
  28. 2.02    Cur()
  29. 2.03    Size()
  30. 2.04    Top()
  31. 2.05    Num()
  32. 2.06    Next()
  33. 2.07    Prev()
  34. 2.08    Last()
  35. 2.09    Insert()
  36. 2.10    Add()
  37. 2.11    Append()
  38. 2.12    Push()
  39. 2.13    DelCur()
  40. 2.14    DelCurObj()
  41. 2.15    Pop()
  42. 2.16    operator[]
  43. 3    custom linked list classes
  44. 4    Stack class
  45. 5    Queue class
  46.  
  47.  
  48.  
  49. 1 what is a linked list:  help for the heapless
  50.  
  51. 1.1 some references
  52.  
  53.   Perhaps the best way to learn about linked lists is to go one of the
  54.   foremost authorities:  In the book "Algorithms + Data Structures =
  55.   Programs" by Niklaus Wirth (1976 Prentice-Hall) in the chapter
  56.   "Dynamic Information Structures" under the section "Linear Lists" is
  57.   a complete tutorial.
  58.  
  59.   The book "Structure and Interpretation of Computer Programs" by
  60.   Abelson, Sussman and Sussman (1985 MIT Press) is based on the Scheme
  61.   language (a LISP dialect) so it's just thick with list stuff.
  62.   Section 2.2.1 should give you a good idea of what's going on here.
  63.  
  64.   The book "An Introduction to Object Oriented Programming and C++" by
  65.   Wiener and Pinson (1988 Addison-Wesley) has the only decent C or C++
  66.   linked list reference I could find inside half an hour.  See section
  67.   6.2: "An Object-Oriented Solution to Generating a Linked List"
  68.  
  69. 1.2 my version
  70.  
  71.   I'm going to assume that you have a pretty good grip on what a heap
  72.   is:  that dynamic memory thing.
  73.  
  74.   Let's say that you have a structure called a DooDad that takes up
  75.   about a thousand bytes.  Let's also say that you need to keep about
  76.   a hundred of them, ordered, in memory for the sake of speed.  Part
  77.   of what the user will be doing with this list is changing the order
  78.   of it.
  79.  
  80.   If you wanted to do a malloc() to store all of this stuff, you'd
  81.   have to ask for about 100,000 bytes.  More than Microsoft C can
  82.   handle in one malloc.  Even if you could get that much memory,
  83.   what's going to happen when you try to move a DooDad from the middle
  84.   and put it at the beginning?  A whole lot of time consuming memory
  85.   copying, that's what (e.g. make a 1000 byte temp copy of element 50;
  86.   shift about 50,000 bytes about 1000 bytes to the right; copy your
  87.   temp to the beginning of your data area).  If you had to insert and
  88.   delete DooDads a lot, things could be unbearably slow.
  89.  
  90.   Now for a linked list approach.  With a tiny struct
  91.  
  92.     struct Node
  93.       {
  94.         Node* PrevNode;
  95.         DooDad* Dat;
  96.         Node* NextNode;
  97.       };
  98.  
  99.   and by allocating memory for your DooDads in thousand byte chunks,
  100.   you can just change the pointers that keep track of the order!
  101.   Voila'!  Everything just the way you want it and without all that
  102.   copying stuff.
  103.  
  104.   Linked lists are also good for bunches of different sized objects.
  105.  
  106. 2 LinkedList class
  107.  
  108.   This linked list class currently uses the model described above.
  109.   Sometimes called a "doubly linked list with nodes", the "doubly"
  110.   referring to the fact that the list has "previous" pointers as well
  111.   as "next" pointers.  The "nodes" part refers to the pointers being
  112.   in its own record and not part of the data record.
  113.  
  114.   There is only one way to create a LinkedList object.  With
  115.   absolutely no parameters (e.g. "LinkedList L;").  When you add any
  116.   data to the list, you must create your data yourself and simply pass
  117.   in a pointer to your data.  You are welcome to add data that is on
  118.   the heap, the stack (local variables or parameters) or the data
  119.   segment (global data).  If you want to add the same object several
  120.   times, you'll just have several nodes pointing to the same place.
  121.  
  122.   When the list is deleted or goes out of scope, it will delete any
  123.   nodes that may still be active.  It will not delete any data that
  124.   you have connected to the list.
  125.  
  126.   All of the pointers are of type "void*".
  127.  
  128. 2.01 Root()
  129.  
  130.   Prototype:  void* Root()
  131.  
  132.   The "current pointer" is changed to point to the last element in the
  133.   list and then the new "current pointer" is returned. Returns NULL if
  134.   there are no elements.
  135.  
  136. 2.02 Cur()
  137.  
  138.   Prototype:  void* Cur()
  139.  
  140.   Returns a pointer to the current element.  Returns NULL if there
  141.   are no elements.
  142.  
  143. 2.03 Size()
  144.  
  145.   Prototype:  Long Size()
  146.  
  147.   Returns the number of elements in the list.
  148.  
  149. 2.04 Top()
  150.  
  151.   Prototype:  Long Top()
  152.  
  153.   Returns the index to the last element.  Equivalent to Size()-1.
  154.  
  155. 2.05 Num()
  156.  
  157.   Prototype:  Long Num()
  158.  
  159.   Returns the index to the current element.  Would be any number from
  160.   0 to Top().
  161.  
  162. 2.06 Next()
  163.  
  164.   Prototype:  void* Next()
  165.  
  166.   The Cur() pointer is changed to point to the next element in the
  167.   list and then the new Cur() is returned.  Returns NULL if there are
  168.   no elements.
  169.  
  170. 2.07 Prev()
  171.  
  172.   Prototype:  void* Prev()
  173.  
  174.   The Cur() pointer is changed to point to the previous element in the
  175.   list and then the new Cur() is returned.  Returns NULL if there are
  176.   no elements.
  177.  
  178. 2.08 Last()
  179.  
  180.   Prototype:  void* Last()
  181.  
  182.   The Cur() pointer is changed to point to the last element in the
  183.   list and then the new Cur() is returned.  Returns NULL if there are
  184.   no elements.
  185.  
  186. 2.09 Insert()
  187.  
  188.   Prototype:  Bool Insert(void* NewRec)
  189.  
  190.   Will insert a new node between the previous element and the current
  191.   element.  On exit, Cur() will point to NewRec.  Returns False if
  192.   there was not enough memory to create a new node.
  193.  
  194. 2.10 Add()
  195.  
  196.   Prototype:  Bool Add(void* NewRec)
  197.  
  198.   Will insert a new node between the current element and the next
  199.   element.  On exit, Cur() will point to NewRec.  Returns False if
  200.   there was not enough memory to create a new node.
  201.  
  202. 2.11 Append()
  203.  
  204.   Prototype:  Bool Append(void* NewRec)
  205.  
  206.   Will stick a new node on the end of the list. On exit, Cur() will
  207.   point to NewRec.  Returns False if there was not enough memory to
  208.   create a new node.
  209.  
  210. 2.12 Push()
  211.  
  212.   Prototype:  Bool Push(void* NewRec)
  213.  
  214.   Will stick a new node on the end of the list. On exit, Cur() will
  215.   point to NewRec.  Returns False if there was not enough memory to
  216.   create a new node.
  217.  
  218.   Note that this works the same way as Append().  This function is
  219.   provided for clarity in your code when you wish to use a linked list
  220.   like a stack (see the Pop() function).
  221.  
  222. 2.13 DelCur()
  223.  
  224.   Prototype:  void DelCur()
  225.  
  226.   I recommend that if this node points to data on the heap (as is most
  227.   often the case) that you should first remove your data from the heap
  228.   using the Cur() pointer.
  229.  
  230.   This function will remove the current node from the list.  It will
  231.   *not* do anything to the data.  On exit, Cur() will point to what
  232.   would have been pointed to by Next().
  233.  
  234. 2.14 DelCurObj()
  235.  
  236.   Prototype:  void DelCurObj()
  237.  
  238.   This function will attempt to "delete" your data.  This will only
  239.   work on data that has been created with "new" and does not have a
  240.   destructor.  After this, it will remove the current node from the
  241.   list. On exit, Cur() will point to what would have been pointed to
  242.   by Next().
  243.  
  244. 2.15 Pop()
  245.  
  246.   Prototype:  void* Pop()
  247.  
  248.   This function will delete the last node in the list and return a
  249.   pointer to what that node pointed to.  On exit Cur() will point to
  250.   the new last element.
  251.  
  252. 2.16 operator[]
  253.  
  254.   Prototype:  void* operator[](Long Index)
  255.  
  256.   This operator will allow your linked list to be treated like an
  257.   array.  If you want a pointer to the fifth element of your
  258.   LinkedList name "L", simply use "L[4]".  A valid function call would
  259.   be something like "memcpy(L[2],L[8],400);".  On exit, Cur() will
  260.   point to the same place.
  261.  
  262. 3 custom linked list classes
  263.  
  264.   A problem with the LinkedList class is that it will only deal with
  265.   void pointers.  That means that if you want to reference a field of
  266.   a struct in a LinkedList, you will first have to take the pointer it
  267.   gives you and cast it to be a pointer to your object and then
  268.   de-reference a field.  What a hassle!  For example:
  269.  
  270.     struct FooType
  271.       {
  272.         Char Name[40];
  273.         Long SSN;
  274.         Long BirthYear;
  275.       };
  276.  
  277.     void Bar()
  278.       {
  279.         LinkedList L;
  280.         L.Add(new FooType);
  281.         strcpy(((FooType*)L.Cur())->Name,"Bob");
  282.         ((FooType*)L.Cur())->SSN=123456789;
  283.         ((FooType*)L.Cur())->BirthYear=1954;
  284.       }
  285.  
  286.   Using the same FooType struct, we could have
  287.  
  288.     CreateLinkedListClass(FooList,FooType);
  289.  
  290.     void Bar()
  291.       {
  292.         FooList L;
  293.         L.Add(*new FooType);
  294.         strcpy(L.Cur().Name,"Bob");
  295.         L.Cur().SSN=123456789;
  296.         L.Cur().BirthYear=1954;
  297.       }
  298.  
  299.   Note that all the parameters in your custom class accept and return
  300.   references instead of pointers.
  301.  
  302. 4 Stack class
  303.  
  304.   The Stack class works exactly the same as the LinkedList class.
  305.   This class is provided for readability's sake only.  If you intend
  306.   to use your linked lisk like a stack, you might want to declare it
  307.   as "Stack FooStack;" rather than "LinkedList FooStack;"
  308.  
  309. 5 Queue class
  310.  
  311.   This class works exactly the same as the LinkedList class *except*
  312.   for the Pop() function.  Pop() will delete the first node in the list
  313.   and return a pointer to what that node pointed to.  On exit Cur()
  314.   will point to the new first element.  So now you see how it behaves
  315.   like a Queue instead of a Stack:  You Push() stuff onto the top and
  316.   Pop() them off the bottom.
  317.